home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1458.dms / var1458.adf / IDCMP / Example2.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  7KB  |  201 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: IDCMP                       Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program explains how to use the IDCMP flag MOUSEMOVE. */
  21.  
  22.  
  23.  
  24. #include <intuition/intuition.h>
  25.  
  26.  
  27.  
  28. struct IntuitionBase *IntuitionBase;
  29.  
  30.  
  31.  
  32. /* Declare a pointer to a Window structure: */ 
  33. struct Window *my_window;
  34.  
  35. /* Declare and initialize your NewWindow structure: */
  36. struct NewWindow my_new_window=
  37. {
  38.   50,            /* LeftEdge    x position of the window. */
  39.   25,            /* TopEdge     y positio of the window. */
  40.   320,           /* Width       320 pixels wide. */
  41.   100,           /* Height      100 lines high. */
  42.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  43.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  44.   CLOSEWINDOW|   /* IDCMPFlags  We will recieve a message when the user:  */
  45.                  /*             selects the Close window gad, or when the */
  46.   MOUSEMOVE,     /*             user moves the mouse.                     */
  47.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  48.   WINDOWCLOSE|   /*             Close Gadget. */
  49.   WINDOWDRAG|    /*             Drag gadget. */
  50.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  51.   WINDOWSIZING|  /*             Sizing Gadget. */
  52.   ACTIVATE|      /*             The window should be Active when opened. */
  53.   
  54.   REPORTMOUSE,   /*             Create MOUSEMOVE messages whenever this */
  55.                  /*             window is active and the mouse is moved. */
  56.  
  57.   NULL,          /* FirstGadget No gadgets connected to this window. */
  58.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  59.   "MOVE THE MOUSE", /* Title    Title of the window. */
  60.   NULL,          /* Screen      Connected to the Workbench Screen. */
  61.   NULL,          /* BitMap      No Custom BitMap. */
  62.   100,           /* MinWidth    We will not allow the window to become */
  63.   50,            /* MinHeight   smaller than 100 x 50, and not bigger */
  64.   400,           /* MaxWidth    than 400 x 200. */
  65.   200,           /* MaxHeight */
  66.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  67. };
  68.  
  69.  
  70.  
  71. /***********************************************************************/
  72. /* Extra information:                                                  */
  73. /* If we set the IDCMP flag MOUSEMOVE then we tell Intuition that we   */
  74. /* are interested in MOUSEMOVE events. However, we must tell Intuition */
  75. /* how and when these messages should be created. There exist two      */
  76. /* ways to do it:                                                      */
  77. /* 1. Set the flag FOLLOWMOUSE in the Activation field in the Gadget   */
  78. /*    structure. We will then recieve messages whenever the gadget is  */
  79. /*    selected and the mouse is moved.                                 */
  80. /* 2. Set the flag REPORTMOUSE in the Flag field in the NewWindow      */
  81. /*    structure. We will then recieve messages whenever the window is  */
  82. /*    active and the mouse is moved. (Showed in this example.)         */
  83. /***********************************************************************/
  84.  
  85.  
  86.  
  87. main()
  88. {
  89.   /* Boolean variable used for the while loop: */
  90.   BOOL close_me;
  91.  
  92.   ULONG class; /* IDCMP flag. */
  93.  
  94.   SHORT x, y;  /* Position of the mouse (x,y). */
  95.  
  96.   BOOL mouse_moved;
  97.  
  98.   /* Pointer to an IntuiMessage structure: */
  99.   struct IntuiMessage *my_message;
  100.  
  101.  
  102.  
  103.   /* Before we can use Intuition we need to open the Intuition Library: */
  104.   IntuitionBase = (struct IntuitionBase *)
  105.     OpenLibrary( "intuition.library", 0 );
  106.   
  107.   if( IntuitionBase == NULL )
  108.     exit(); /* Could NOT open the Intuition Library! */
  109.  
  110.  
  111.  
  112.   /* We will now try to open the window: */
  113.   my_window = (struct Window *) OpenWindow( &my_new_window );
  114.   
  115.   /* Have we opened the window succesfully? */
  116.   if(my_window == NULL)
  117.   {
  118.     /* Could NOT open the Window! */
  119.     
  120.     /* Close the Intuition Library since we have opened it: */
  121.     CloseLibrary( IntuitionBase );
  122.  
  123.     exit();  
  124.   }
  125.  
  126.  
  127.  
  128.   /* We have opened the window, and everything seems to be OK. */
  129.  
  130.   printf("Move the mouse!\n");
  131.  
  132.  
  133.  
  134.   close_me = FALSE;
  135.  
  136.   /* Stay in the while loop until the user has selected the Close window */
  137.   /* gadget: */
  138.   while( close_me == FALSE )
  139.   {
  140.     mouse_moved = FALSE;
  141.  
  142.  
  143.     /* Wait until we have recieved a message: */
  144.     Wait( 1 << my_window->UserPort->mp_SigBit );
  145.  
  146.     /* As long as we can collect messages successfully we stay in the */
  147.     /* while-loop: */
  148.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  149.     {
  150.       /* After we have successfully collected the message we can read */
  151.       /* it, and save any important values which we maybe want to check */
  152.       /* later: */
  153.       class = my_message->Class;  /* IDCMP flag. */
  154.       x     = my_message->MouseX; /* X position of the mouse. */
  155.       y     = my_message->MouseY; /* Y position of the mouse. */
  156.  
  157.  
  158.       /* After we have read it we reply as fast as possible: */
  159.       /* REMEMBER! Do never try to read a message after you have replied! */
  160.       /* (Some other process has maybe changed it.) */
  161.       ReplyMsg( my_message );
  162.  
  163.  
  164.       /* Check which IDCMP flag was sent: */
  165.       switch( class )
  166.       {
  167.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  168.                close_me=TRUE;
  169.                break;
  170.         
  171.         case MOUSEMOVE:    /* The user moved the mouse. */
  172.                mouse_moved = TRUE;
  173.                break;
  174.       }
  175.     }
  176.     
  177.     if( mouse_moved )
  178.     {
  179.       /* Since we recieve so many messages when the mouse is moved, we */
  180.       /* respond first when the mouse has halted. Print out the mouse  */
  181.       /* position relative to the top left corner of the window.       */
  182.       printf("New position: (%d, %d)\n", x, y);
  183.     }
  184.   }
  185.  
  186.  
  187.  
  188.   /* Close the window: */
  189.   CloseWindow( my_window );
  190.  
  191.  
  192.  
  193.   /* Close the Intuition Library: */
  194.   CloseLibrary( IntuitionBase );
  195.   
  196.   /* THE END */
  197. }
  198.  
  199.  
  200.  
  201.